package cn.genmer.test.security.recordlog.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect // 1.表明这是一个切面类
@Component
public class PerformanceLogAspect {

    // 3. 环绕通知
    @Around("@annotation(cn.genmer.test.security.recordlog.annotation.PerformanceLog)")
    public Object logAround(ProceedingJoinPoint joinPoint){
        // 获取方法名称
        String methodName = joinPoint.getSignature().getName();
        // 获取入参
        Object[] param = joinPoint.getArgs();

        StringBuilder sb = new StringBuilder("进入[" + methodName + "]方法,参数为:");
        for(Object o : param){
            sb.append(o + "; ");
        }
        // 继续执行方法
        try {
            long start = System.currentTimeMillis();
            Object proceed = joinPoint.proceed();
            System.out.println(sb.append("耗时：").append(System.currentTimeMillis() - start).append("ms").toString());
            return proceed;

        } catch (Throwable throwable) {

            throwable.printStackTrace();

        }
        System.out.println(methodName + "方法执行结束");
        return null;
    }
}